class CulturalHeritage::CulturalInstitution < ActiveRecord::Base
  validates :name, :presence => {:message => "El nombre de la Institucion Cultural es requerido"}
  belongs_to :obj_department, :class_name => 'Shared::Department',  :foreign_key => 'department'
  belongs_to :obj_province, :class_name => 'Shared::Province',  :foreign_key => 'province'
  belongs_to :obj_district, :class_name => 'Shared::District',  :foreign_key => 'district'
  default_scope where("cultural_heritage_cultural_institutions.deleted = 0")
  has_attached_file :avatar  
  validates :name,:uniqueness => true
  has_many :list_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'culturalInstitution'
  acts_as_gmappable :lat => 'latitude', :lng =>'longitude',:process_geocoding => :geocode?


  def geocode?
    !(address.blank? || (!latitude.blank? && !longitude.blank?))
  end
  def gmaps4rails_address
  #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
    "#{self.address}"
  end

  def define_info_window_for_marker_for_maps(root_url)
    if (avatar_file_name.blank?)
        image = root_url << "images/pukara_logo.png"        
      else        
        image = avatar.url              
      end
      referencia = "<b><a href = '/cultural_heritage/cultural_institutions/"
      return referencia+id.to_s+"'>"+name.to_s+"</a></b>"+ "<br/>"+description.to_s+'<br/><br/> <img  src='+image+ "  style='thumb'"+"width='100' height='100'"+"/></div>"
  end

  def define_info_window_for_marker_for_maps_cultural_heritage(root_url,cultural_heritage_id)
    cultural_heritage = CulturalHeritage::CulturalHeritage.find(cultural_heritage_id)
    if (avatar_file_name.blank?)
      image = root_url << "images/pukara_logo.png"
    else
      image = avatar.url
    end
    institucion = "<h3><a href = '/cultural_heritage/cultural_institutions/"+
                    id.to_s+"'>"+
                    name.to_s+"</a></h3>"
    mensaje = "<p>Patrimonio ubicado en: <h3>" + institucion+ "</h3></p>"
    referencia = "<b><a href = '/cultural_heritage/cultural_heritages/"

    return referencia+cultural_heritage.id.to_s+"'>"+cultural_heritage.name.to_s+"</a></h2><p>"+mensaje+"</p>"+
            "<p>"+cultural_heritage.description.to_s+"</p><img src="+image+
            "  style='thumb'"+"width='100' height='100'"+"/></div>"
    #return referencia+id.to_s+"'>"+name.to_s+"</a></b>"+ "<br/>"+description.to_s+'<br/><br/> <img  src='+image+'  width=\"100\" height=\"100\">'
  end

  def test_list
    count = 0
    if ! list_cultural_heritage.blank?
      list_cultural_heritage.each do |cultural_heritage|
        if cultural_heritage.typeCulturalInstitution == 2          
          count = count + 1
        end
      end
      puts " "
      puts " "
      puts " "
      puts " "
      puts " "
      puts " "
      puts "Esto vale count "+count.to_s
      puts "Esto vale size "+list_cultural_heritage.size.to_s
      puts " "
      puts " "
      puts " "
      puts " "
      puts " "
      puts " "
      if count == list_cultural_heritage.size
        return false
      else
        return true
      end
    end    
  end  
end
